﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Money.Data;
using Money.Interfaces;
using One.Framework;
using System.Web.Security;
using Money.Entities;

/// <summary>
/// Summary description for WebSession
/// </summary>
public class WebSession
{
    public static User CurrentUser
    {
        get
        {
            if(HttpContext.Current.Session["CurrentUser"] == null)
            {
                try
                {
                    IUserService userService = ServiceFactory.Create<IUserService>();
                    HttpContext.Current.Session["CurrentUser"] = userService.GetUserByUsername(HttpContext.Current.User.Identity.Name);
                }
                catch
                {
                    Logger.Info("User have been expired and not got back. RedirectUrl: " + FormsAuthentication.DefaultUrl);
                    HttpContext.Current.Response.Redirect(FormsAuthentication.LoginUrl);
                }
            }
            return (User)HttpContext.Current.Session["CurrentUser"];
        }
        set
        {
            HttpContext.Current.Session["CurrentUser"] = value;
        }
    }
}
